package com.sun.androidexerciseproject.function.permission;

import android.Manifest;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;

import com.sun.androidexerciseproject.function.R;
import com.sun.androidexerciseproject.function.permission.grantor.PermissionListener;
import com.sun.androidexerciseproject.function.permission.grantor.PermissionsUtil;
import com.sun.androidexerciseproject.function.permission.ui.permission.PermissionFragment;
import com.sun.androidexerciseproject.function.util.LogUtil;

public class PermissionTestActivity extends AppCompatActivity {
    private static final String[] PERMISSIONS = new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.permission_activity);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.container, PermissionFragment.newInstance())
                    .commitNow();
        }
        getAccount();

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!PermissionsUtil.hasPermission(this, PERMISSIONS)) {
            PermissionsUtil.requestPermission(this, new PermissionListener() {
                @Override
                public void permissionGranted(@NonNull String[] permission) {
                    LogUtil.log("permissionGranted");
                }

                @Override
                public void permissionDenied(@NonNull String[] permission) {
                    LogUtil.log("permissionDenied");
                }
            }, PERMISSIONS);
        }
    }

    private void getAccount() {
        AccountManager manager = AccountManager.get(this);
        Account[] accounts = manager.getAccounts();
        if (null != accounts) {
            for (Account account : accounts) {
                LogUtil.log("getAccount account " + account);
            }
        }
    }
}
